package org.minuframework.file.upload;

import java.io.File;
import java.util.Date;

import org.minuframework.lang.StringUtils;
import org.minuframework.time.TimeU;
import org.minuframework.utils.ParamUtils;
import org.minuframework.utils.RandomUtils;

@Deprecated
public abstract class AbstractFileBuilder{
    
    protected static final String   DEFAULT_FILE_CODE   = "yyMMddHHmmssSSS";
    protected static final String   DEFAULT_FILE_SUFFIX = ".jpg";
    protected String                charset;
    protected FileParamerConfigurer fileParamerConfigurer;
    protected FileTypesConfigurer   fileTypesConfigurer;
    
    public AbstractFileBuilder(FileParamerConfigurer fileParamerConfigurer, FileTypesConfigurer fileTypesConfigurer, String charset){
        this.fileParamerConfigurer = fileParamerConfigurer;
        this.fileTypesConfigurer = fileTypesConfigurer;
        this.charset = charset;
    }
    
    public String getCharset(){
        return charset;
    }
    
    public FileParamerConfigurer getFileParamerConfigurer(){
        return fileParamerConfigurer;
    }
    
    public FileTypesConfigurer getFileTypesConfigurer(){
        return fileTypesConfigurer;
    }
    
    public File getFile(FileUploadType type){
        return new File(fileParamerConfigurer.getFile(type));
    }
    
    public File getFile(String fileName, FileUploadType type){
        return new File(fileParamerConfigurer.getFile(fileName, type));
    }
    
    public File getFile(String fileName, String type){
        return new File(fileParamerConfigurer.getFile(fileName, type));
    }
    
    public static String randomUUId(){
        String code = TimeU.formatDateTime(new Date(), DEFAULT_FILE_CODE);
        return RandomUtils.getFK(13) + code + RandomUtils.getFK(12); // 文件名
    }
    
    public static String randomUUId(String fileName){
        StringBuilder result = new StringBuilder();
        
        if (StringUtils.isNotEmpty(fileName)) {
            String code = TimeU.formatDateTime(new Date(), DEFAULT_FILE_CODE);
            String suffix = ParamUtils.suffix(fileName);
            String param = RandomUtils.getFK(13) + code + RandomUtils.getFK(12) + suffix; // 文件名
            result.append(param);
        }
        
        return result.toString();
    }
    
    public static boolean isFile(File file){
        if (file == null) { return false; }
        if (!file.isDirectory() && !file.exists()) { return file.mkdirs(); }
        return true;
    }
    
    public static boolean isNotFile(File file){
        if (file == null || !file.isDirectory() || !file.exists()) { return true; }
        return false;
    }
    
}
